package com.ruoyi.score.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.score.domain.Score;
import com.ruoyi.score.mapper.CourseMapper;
import com.ruoyi.score.domain.Course;
import com.ruoyi.score.service.ICourseService;

/**
 * 成绩Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-20
 */
@Service
public class CourseServiceImpl implements ICourseService 
{
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 查询成绩
     * 
     * @param id 成绩主键
     * @return 成绩
     */
    @Override
    public Course selectCourseById(Long id)
    {
        return courseMapper.selectCourseById(id);
    }

    /**
     * 查询成绩列表
     * 
     * @param course 成绩
     * @return 成绩
     */
    @Override
    public List<Course> selectCourseList(Course course)
    {
        return courseMapper.selectCourseList(course);
    }

    /**
     * 新增成绩
     * 
     * @param course 成绩
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCourse(Course course)
    {
        int rows = courseMapper.insertCourse(course);
        insertScore(course);
        return rows;
    }

    /**
     * 修改成绩
     * 
     * @param course 成绩
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCourse(Course course)
    {
        courseMapper.deleteScoreByCouresid(course.getId());
        insertScore(course);
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除成绩
     * 
     * @param ids 需要删除的成绩主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCourseByIds(Long[] ids)
    {
        courseMapper.deleteScoreByCouresids(ids);
        return courseMapper.deleteCourseByIds(ids);
    }

    /**
     * 删除成绩信息
     * 
     * @param id 成绩主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCourseById(Long id)
    {
        courseMapper.deleteScoreByCouresid(id);
        return courseMapper.deleteCourseById(id);
    }

    /**
     * 新增学科课程信息
     * 
     * @param course 成绩对象
     */
    public void insertScore(Course course)
    {
        List<Score> scoreList = course.getScoreList();
        Long id = course.getId();
        if (StringUtils.isNotNull(scoreList))
        {
            List<Score> list = new ArrayList<Score>();
            for (Score score : scoreList)
            {
                score.setCouresid(id);
                list.add(score);
            }
            if (list.size() > 0)
            {
                courseMapper.batchScore(list);
            }
        }
    }
}
